<?php

##################################################
#
# Copyright (c) 2004-2006 OIC Group, Inc.
# Written and Designed by James Hunt
#
# This file is part of Exponent
#
# Exponent is free software; you can redistribute
# it and/or modify it under the terms of the GNU
# General Public License as published by the Free
# Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# GPL: http://www.gnu.org/licenses/gpl.txt
#
##################################################

/**
* Change log
* Jul 11, 2011 - trungdt - Now support normal user select file by open this action
*/
include_once('../../../exponent.php');
if (filemanagermodule::_CanUpload()) {
	
	// Apr, 2012 - Load language
	$i18n = exponent_lang_loadFile("/modules/filemanagermodule/common.php");
	
	// Jul 11, 2011 - trungdt
	// check permission if only a normal user
	$normalUserOnly=filemanagermodule::_CheckNormalUser();
	$sql_where="";
	if (!filemanagermodule::_CanApprove())
	{
		$sql_where.=" AND poster = {$user->id}";
	}
	
	$collection = null;
	if (isset($_GET['id']) && !$normalUserOnly) { // force to use public collection
		$collection = $db->selectObject('file_collection','id='.intval($_GET['id']));
	}
	if (!$collection) {
		$collection->id = 0;
		$collection->name = $i18n['default_account_title'];
		$collection->description = $i18n['default_account_des'];
	}
	//$loc = exponent_core_makeLocation('filemanagermodule');
	// Jul 10, 2011 - trungdt - Using _getLoc to ensure we can get permission check
	$loc=filemanagermodule::_GetLoc();
	
	//exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);

	$template = new template('filemanagermodule','_picker');

	$template->assign('collection',$collection);

	// Jul 11, 2011 - trungdt - if normal user, dont need to load more collection
	$collections=array();
	if (!$normalUserOnly){
		$collections = $db->selectObjects('file_collection');
	}
	$template->assign('normalUserOnly',$normalUserOnly);
	$template->assign('collections',$collections);
	
	// chia trang - 21/7/2009
		if (isset($_REQUEST['image_per_line']))
	{
		$ispl=$_REQUEST['image_per_line'];
		if (!(($ispl >=5) && ($ispl <=10)))
			$ispl=7;
	}
	else
		$ispl=7;	
	
	$max_item_per_page = $ispl * 5;
	
	// how many pages?
	$pages=$db->countObjects('file'," collection_id= {$collection->id} {$sql_where}");
	$template->assign('item_count',$pages);
	if (fmod($pages,$max_item_per_page) > 0)
			$pages = $pages / $max_item_per_page +1;
	else
		$pages = $pages / $max_item_per_page;
	
	if (isset($_REQUEST['page']))
	$page=$_REQUEST['page'];
  else
	$page = 1;
	if ($page <0) 
		$page=1;
	if ($page > $pages)
		$page = $pages;
	if (!($page >0) && ($page<=$pages))
		$page=1;
	
	// items: start and end
	
	$start_item = $max_item_per_page * ($page-1);
	
	$files = $db->selectObjects('file',"collection_id={$collection->id} {$sql_where} ORDER BY id DESC LIMIT " . $start_item . "," . $max_item_per_page );
	
	// tạo mảng các trang
	$temp=array();
	for ($i=0;$i<$pages-1;$i++)
		$temp[$i+1]=$i+1;
	$pages=$temp;	
	
	// ABSOFT Jul 4 2009
	// Immanuel192
	// trim filename for all files
	// and get file size to calculate  folder size
	$folder_size=0;
	foreach ($files as $key=>$value)
	{
	  if ($files[$key]->name !='')
	   $files[$key]->temp_filename=$files[$key]->name;
	  else
	   $files[$key]->temp_filename=$files[$key]->filename;
		if (strlen($files[$key]->temp_filename) > 15)
		{
			$files[$key]->temp_filename=substr($files[$key]->temp_filename,0,15) . "...";
		}
		// calculate folder size
		$folder_size+=$files[$key]->filesize;
	}
	
	// okies, here w calculate folder size
	$folder_size_suffix="bytes";
	if ($folder_size > 1024)
		{
			$folder_size=(float)($folder_size)/(float)1024;
			$folder_size_suffix="KB";
		}
	if ($folder_size > 1024)
		{
			$folder_size=(float)($folder_size)/(float)1024;
			$folder_size_suffix="MB";
		}
	if ($folder_size > 1024)
		{
			$folder_size=(float)($folder_size)/(float)1024;
			$folder_size_suffix="GB";
		}
	$folder_size=round($folder_size,2).$folder_size_suffix;
	if (!defined('SYS_SORTING')) require_once(BASE.'subsystems/sorting.php');
	usort($files,'exponent_sorting_byPostedDescending');
	$template->assign('files',$files);
	$template->assign('folder_size',$folder_size);
	$template->assign('numfiles',count($files));
	$template->assign('id',$collection->id);
	$template->assign('page',$page);
	$template->assign('pages',$pages);
	
	$template->assign('session_id',session_id());
	$template->assign('image_per_line',$ispl);

	if (isset($_GET['highlight_file'])) {
		$template->assign('highlight_file',$_GET['highlight_file']);
	} else {
		$template->assign('highlight_file',0);
	}
	// Immanuel192 Add this line to set Flow Action; 7/22/2009
  exponent_flow_set(SYS_FLOW_NONE,SYS_FLOW_ACTION);

	$template->output();
} else {
	echo exponent_http_error(403);
}

?>
